﻿using System;
using System.Collections.Specialized;
using System.Configuration;
using System.Net.Configuration;
using System.Net.Mail;
using System.Web;


namespace SystemCore.Helper
{
    public static class Email
    {

        /// <summary>
        /// Sends the mail.
        /// </summary>
        /// <param name="from">From.</param>
        /// <param name="to">To.</param>
        /// <param name="subject">The subject.</param>
        /// <param name="body">The body.</param>
        /// <param name="isBodyHtml">if set to <c>true</c> [is body HTML].</param>
        /// <param name="attachment">The attachment.</param>
        /// <param name="error">The error.</param>
        /// <returns></returns>
        public static bool SendMail(string from, string to, string subject, string body, bool isBodyHtml,  out string error)
        {
            bool isSuccess = false;

            try
            {
                error = "";

                //(1) Create the MailMessage instance 
                MailMessage mm = new MailMessage();

                //(2) Assign the MailMessage's properties 
                mm.From = new MailAddress(from);
                string[] strToMultiple = to.Split(';');

                if (strToMultiple.Length > 0)
                {
                    for (int i = 0; i < strToMultiple.Length; i++)
                    {
                        if (!string.IsNullOrEmpty(strToMultiple[i]))
                        {
                            mm.To.Add(new MailAddress(strToMultiple[i]));
                        }
                    }
                }

                mm.Subject = subject;
                mm.Body = body;
                mm.IsBodyHtml = isBodyHtml;

                //(3) Create the SmtpClient object 
                SmtpClient smtp = new SmtpClient();
                //smtp.Timeout = 100000;

                //(5) Send the MailMessage (will use the Web.config settings) 
                smtp.Send(mm);

                isSuccess = true;

            }
            catch (SmtpException smtpEx)
            {
                error = smtpEx.ToString();
            }
            catch (Exception generalEx)
            {
                error = generalEx.ToString();
            }

            return isSuccess;
        }

        /// <summary>
        /// Sends the mail from definition.
        /// </summary>
        /// <param name="from">From.</param>
        /// <param name="to">To.</param>
        /// <param name="subject">The subject.</param>
        /// <param name="dic">The dic.</param>
        /// <param name="bodyFileName">Name of the body file.</param>
        /// <param name="isBodyHtml">if set to <c>true</c> [is body HTML].</param>
        /// <param name="error">The error.</param>
        /// <returns></returns>

        /// <summary>
        /// Gets the SMTP section.
        /// </summary>
        /// <returns></returns>
        public static SmtpSection GetSmtpSection()
        {
            SmtpSection smtpSec = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");

            return smtpSec;
        }
    }
}
